package com.cashmate.struts;

import com.cashmate.hibernate.dao.EmployeeDAO;
import com.cashmate.hibernate.dao.TypeofcollateralDAO;
import com.cashmate.hibernate.pojo.Employees;
import com.cashmate.hibernate.pojo.Typeofcollateral;
import com.cashmate.hibernate.pojo.Usertypes;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;

public class EditTypeofcollateral extends ActionSupport {

    private static Logger logger = Logger.getLogger(EditProducts.class);
    private String oper;
    private String id;
    private String type;
    private final TypeofcollateralDAO colldao = (TypeofcollateralDAO) ServletActionContext.getServletContext().getAttribute("typeofcollateralDAO");
    private final EmployeeDAO empDao = (EmployeeDAO) ServletActionContext.getServletContext().getAttribute("employeeDAO");
    private Employees emps = (Employees) ServletActionContext.getRequest().getSession().getAttribute("employee");
    private Usertypes ut = (Usertypes) ServletActionContext.getRequest().getSession().getAttribute("usertype");

    @Action(value = "/edit-typeofcollateral", results = {
        @Result(name = "duplicate", location = "../error/formError.jsp"),
        @Result(name = "error", location = "../error/messages.jsp")
    })
    @Override
    public String execute() {
        try {
            Typeofcollateral colltype = null;
            if (oper.equals("add")) {
                if (colldao.hasDuplicate(type)) {
                    addActionError("ERROR : Collateral Type " + type + " already exists.");
                    return "duplicate";
                }

                colltype = new Typeofcollateral(type);
                colldao.insert(colltype);
                logger.info(ut.getType() + " (" + emps.getIdnumber() + ") added a new Collateral type: {Type of collateral ID:" + id + ", type:" + type + "}");

            } else if (oper.equals("edit")) {
                colltype = colldao.get(Integer.parseInt(id));
                if (!colltype.getType().equals(type)) {
                    if (colldao.hasDuplicate(type)) {
                        addActionError("ERROR : Collateral Type " + type + " already exists.");
                        return "duplicate";
                    }
                }

                String oldtype = colltype.getType();
                colltype.setType(type);
                colldao.update(colltype);

                logger.info(ut.getType() + " (" + emps.getIdnumber() + ") updated Collateral Type(" + oldtype + ") with new values: Type of collateral ID:" + id + ", type:" + type + "}");

            } else if (oper.equals("del")) {
                colltype = colldao.get(Integer.parseInt(id));
                colldao.delete(Integer.parseInt(id));
                logger.info(ut.getType() + " (" + emps.getIdnumber() + ") deleted Type of collateral: " + colltype.getType() + "}");

            }
        } catch (Exception e) {
            e.printStackTrace();;
            if (emps != null) {
                logger.fatal("(" + emps.getIdnumber() + "):" + e.getLocalizedMessage(), e);
            } else {
                logger.fatal(e.getLocalizedMessage(), e);
            }
            addActionError(e.getLocalizedMessage());
            return ERROR;
        }
        return NONE;
    }

    public void setId(String id) {
        this.id = id;
    }

    public void setType(String type) {
        this.type = type;
    }

    public void setOper(String oper) {
        this.oper = oper;
    }
}
